﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using JX.Infrastructure;
using JX.Infrastructure.Common;
using JX.Infrastructure.Framework.Authorize;
using Microsoft.AspNetCore.Mvc;

namespace JXWebSite.Areas.Admin.Controllers
{
	[Area("Admin")]
	[Route("Admin/[controller]/[action]")]
	[AdminAuthorize]
	public class TemplateController : Controller
	{
		private string _SkinStyleDir = "/Skin";
		public TemplateController()
		{

		}

		#region 风格管理
		/// <summary>
		/// 初始化风格管理页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,StyleManage")]
		public IActionResult StyleManage()
		{
			string strNav = string.Empty;
			string strParentDir = "/";
			var dir = Utility.Request("Dir", string.Empty, false);
			if (!string.IsNullOrEmpty(dir))
			{
				string[] strArray = dir.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
				for (int i = 0; i < strArray.Length; i++)
				{
					string strDirPath = string.Empty;
					for (int j = 0; j <= i; j++)
					{
						strDirPath += "/" + strArray[j];
					}
					strNav += "<a href=\"/Admin/Template/StyleManage?Dir=" + WebUtility.UrlEncode(strDirPath) + "\">" + strArray[i] + "</a>";
				}
				if (dir.LastIndexOf("/", StringComparison.Ordinal) > 0)
				{
					strParentDir = dir.Remove(dir.LastIndexOf("/", StringComparison.Ordinal), dir.Length - dir.LastIndexOf("/", StringComparison.Ordinal));
				}
			}
			if (string.IsNullOrEmpty(strNav))
			{
				strNav = "<a href=\"/Admin/Template/StyleManage>/</a>";
			}

			List<string> dirList = new List<string>();
			DirectoryInfo skinDirInfo = new DirectoryInfo(FileHelper.WebRootPath + _SkinStyleDir);
			if (skinDirInfo.Exists)
			{
				foreach (DirectoryInfo skinSubDirInfo in skinDirInfo.GetDirectories("*", SearchOption.AllDirectories))
				{
					if (!skinSubDirInfo.FullName.Contains(ConfigHelper.Get<SiteOptionConfig>().LabelDirConfig)
						&& !skinSubDirInfo.FullName.Contains(ConfigHelper.Get<SiteOptionConfig>().PagerLabelDirConfig))
					{
						string text = (skinSubDirInfo.FullName.Remove(0, skinDirInfo.FullName.Length) + "/").Replace(@"\", "/");
						dirList.Add(text);
					}
				}
			}
			ViewBag.NavText = strNav;
			ViewBag.ParentDir = strParentDir;
			return View(dirList);
		}
		/// <summary>
		/// 得到风格管理的目录列表
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,StyleManage")]
		public IActionResult GetStyleManageList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			var Dir = Utility.Query("Dir",string.Empty,false);
			string currentDirectory = FileHelper.WebRootPath + _SkinStyleDir + Dir + "/";
			currentDirectory = currentDirectory.Replace("/", @"\");
			currentDirectory = currentDirectory.Replace(@"\\", @"\");
			List<DirDetail> result = FileHelper.GetDirectoryInfos(currentDirectory, FileMethod.All);
			var pagerModel = new PagerModel<DirDetail>(PageNum, PageSize, result.Count, result);
			return Json(pagerModel);
		}
		#endregion
	}
}